package com.epk.pic.service;

import com.epk.pic.mapper.PicMapper;
import com.epk.pic.mapper.TargetMapper;
import com.epk.pic.po.PicContents;
import com.epk.pic.po.PicTargetMain;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PicService {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + File.separator + "MM-dd");

    @Value("${files.fileOutPath}")
    private String fileOutPath;

    @Value("${files.webRoot}")
    private String webRoot;

    @Value("${files.imgApi}")
    private String imgApi;

    @Value("${secret.adminPwd}")
    private String adminPwd;

    @Value("${query.pageSize}")
    private Integer pageSize;

    @Autowired
    private PicMapper picMapper;

    @Autowired
    private TargetMapper targetMapper;

    private static PicService ts;

    @PostConstruct
    public void init() {
        ts = this;
    }

    public static String getFileOutPath() {return ts.fileOutPath;}
    public static String getAdminPwd() {return ts.adminPwd;}
    public static Integer getPageSize() {return ts.pageSize;}



    public PicContents uploadFIle(MultipartFile file) throws IOException {
        PicContents picContents = new PicContents();
        String suffix = StringUtils.getFilenameExtension(file.getOriginalFilename());
        suffix = suffix == null || "null".equals(suffix) ? "jpg" : suffix;
        String md5 = DigestUtils.md5Hex(file.getInputStream());
        //判断是否存在，存在的话，就不需要重复上传
        PicContents old = picMapper.selectContentsByMd5(md5);
        File newFile = null;
        if (old == null) {
            String date = sdf.format(new Date());
            String outFile = fileOutPath + File.separator + date + File.separator + md5 + "." + suffix;
            newFile = new File(outFile);
        } else {
            newFile = new File(old.getFilePath());
        }
        File dir = newFile.getParentFile();
        if (!dir.exists()) { //如果不存在
            dir.mkdirs();
        }
        if (!newFile.exists()) {
            file.transferTo(newFile);
        }
        picContents.setFilePath(newFile.getPath());
        picContents.setFileMd5(md5);
        picContents.setFileName(file.getOriginalFilename());
        return picContents;
    }


    public List<String> getTargetGroup(Map<String, String> targets, Integer searchSub) {
        List<String> targetGroup = new ArrayList<>();
        if(targets !=null){
            for (String s : targets.keySet()) {
                String s1 = targets.get(s);
                String[] split = s1.split(";");
                List<PicTargetMain> picTargetMains = targetMapper.selectTargetAndSub(searchSub, s);
                for (PicTargetMain picTargetMain : picTargetMains) {
                    for (String s2 : split) {
                        targetGroup.add(picTargetMain.getTargetName() + ":" + s2);
                    }
                }
            }
        }
        return targetGroup;
    }

    public static String getPicApiPath() {
        return ts.webRoot + ts.imgApi + "/";
    }

}
